package com.esxy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.esxy.dao.TbUserMapper;
import com.esxy.entity.TbUser;
import com.esxy.entity.TbUserExample;
import com.esxy.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Service(interfaceClass = TbUserService.class)
public class TbUserServiceImpl implements TbUserService {

    @Autowired(required = false)
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser getUserByUsername(String username, String password) {
        // 创建查询条件
        TbUserExample example = new TbUserExample();
        example.createCriteria()
                .andUsernameEqualTo(username)
                .andPasswordEqualTo(password); // 同时验证用户名和密码

        List<TbUser> userList = tbUserMapper.selectByExample(example);

        // 如果查询结果不为空且正好有一条记录，则返回该用户
        if (userList != null && userList.size() == 1) {
            return userList.get(0);
        }
        // 其他情况都返回null（用户不存在或密码错误）
        return null;
    }
}